package com.key.oa.activity;

import com.key.keyleaderoa.R;
import com.key.oa.http.HttpUncheck;
import com.key.oa.util.SysParams;
import com.key.oa.util.Util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class UncheckedActivity extends Activity implements OnClickListener {

	public ProgressDialog pDialog = null;
	
	private TextView budget = null, content = null, time = null, 
					department = null, name = null, businessname = null ;
	
	private EditText suggestionText = null; 
	
	private ImageButton passButton = null, unpassButton = null;
	
	private int pos = -1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.key_unchecked_menu);
        
        if( SysParams.uncheckedActivity != null ) {
        	SysParams.uncheckedActivity.finish();
        }
        SysParams.uncheckedActivity = this;
        
        initPassButton();
        initUnpassButton();
        initTextView();
        initSuggestion();
        
        Bundle bundle = new Bundle();
        bundle = getIntent().getExtras();  
        pos = Integer.parseInt(bundle.getString("pos"));
        
        if( pos < 0 || pos > Util.uncheckdata.size() ) {
        	Util.toastPopWindow(this, "系统故障");
			Intent gameIntent = new Intent(this,MainActivity.class);
			this.startActivity(gameIntent);
			this.finish();	
        }
        
        setTextView();
    }
	
	private void initSuggestion() {
		suggestionText = (EditText) findViewById(R.id.suggestionText);
	}
	
	private void initPassButton() {
		
		passButton = (ImageButton) findViewById(R.id.sureUncheckedButton);
		passButton.setOnClickListener(this);
		passButton.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.key_pass));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.key_pass_down));
				}
				return false;
			}
		});
	}
	
	private void initUnpassButton() {
		
		unpassButton = (ImageButton) findViewById(R.id.cannelUncheckedButton);
		unpassButton.setOnClickListener(this);
		unpassButton.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.key_unpass));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.key_unpass_down));
				}
				return false;
			}
		});
	}

	private void initTextView() {
		
		time = (TextView) findViewById(R.id.timeText);
		budget = (TextView) findViewById(R.id.budgetText);
		content = (TextView) findViewById(R.id.contentText);
		department = (TextView) findViewById(R.id.departmentText);
		name = (TextView) findViewById(R.id.nameText);
		businessname = (TextView) findViewById(R.id.businessnameText);
	}
	
	private void setTextView() {
		
		time.setText((String)Util.uncheckdata.get(pos).getUnchecked("StartTime"));
		budget.setText((String)Util.uncheckdata.get(pos).getUnchecked("cost_budget"));
		content.setText((String)Util.uncheckdata.get(pos).getUnchecked("Classfication"));
		department.setText((String)Util.uncheckdata.get(pos).getUnchecked("depname"));
		name.setText((String)Util.uncheckdata.get(pos).getUnchecked("name"));
		businessname.setText((String)Util.uncheckdata.get(pos).getUnchecked("BusinessIdName"));
	}
	
	public void onClick(View v) {

		Intent gameIntent = null;
		switch (v.getId()) {
		case R.id.sureUncheckedButton:
			if( sendUncheckedSureHttpRequest() == false ) {
				Util.toastPopWindow(this,"失败,请检查网络等信息");
			}
			gameIntent = new Intent(this,MainActivity.class);
			this.startActivity(gameIntent);
			this.finish();
			break;
		case R.id.cannelUncheckedButton:
			sendUncheckedCannelHttpRequest();
			gameIntent = new Intent(this,MainActivity.class);
			this.startActivity(gameIntent);
			this.finish();
			break;
		default:
			break;
		}
	}
	
	private String getSuggestionText() {
		
		String suggestion = suggestionText.getText().toString().trim();
		
		if( suggestion == null) {
			return "";
		}
		
		return suggestion;
	}
	
	private boolean sendUncheckedSureHttpRequest() {
		
		pDialog = new ProgressDialog(UncheckedActivity.this);
		pDialog.setTitle("提示");
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setMessage("正在发送通过的信息");
		pDialog.setIndeterminate(true);
		pDialog.show();
		
		String result = HttpUncheck.get( Util.serverdata.getUriPass(getSuggestionText(),pos) );
		if( result == null || result.length() == 0 ) {
			//Util.toastPopWindow(this,"通过信息失败");
			Message msg = new Message();
			msg.what = 1;
			handler.sendMessage(msg);
			if (pDialog != null) {
				pDialog.cancel();
			}
			return false;
		}
		Util.uncheckdata.clear();
		return true;
	}
	
	public Handler handler = new Handler() {
		public AlertDialog.Builder dg = null;
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				dg = new AlertDialog.Builder(UncheckedActivity.this);
				dg.setTitle("发送失败");
				String message = "请检查网络等信息";
				dg.setMessage(message);
				dg.show();
				break;
			default:
				break;
			}
		}
	};
	
	private boolean sendUncheckedCannelHttpRequest() {
		
		pDialog = new ProgressDialog(UncheckedActivity.this);
		pDialog.setTitle("提示");
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setMessage("正在发送中不通过的信息");
		pDialog.setIndeterminate(true);
		pDialog.show();
		
		String result = HttpUncheck.get( Util.serverdata.getUriUnpass(getSuggestionText(),pos) );
		if( result == null || result.length() == 0 ) {
			//Util.toastPopWindow(this,"不通过信息失败");
			Message msg = new Message();
			msg.what = 1;
			handler.sendMessage(msg);
			if (pDialog != null) {
				pDialog.cancel();
			}
			return false;
		}
		Util.uncheckdata.clear();
		return true;
	}
}
